import request from '@/libs/request';
let _self;
export const index =  {
	props: {
		show: {type: Boolean,default: false},
		title: {type: String,default: ""},
		optData:{type: Object,default: {}},
	},
	data() {
		return {
			host:process.env.HOST,
			showBox:false,
			boxTitle:this.title,
			areaList:[],
			areaIndex:0,
			syncList:[],
			syncIndex:0,
			codeList:[],
			codeIndex:0,
			payWay: [{text: '否',value: 0}, {text: '是',value: 1}],
			address:{"address":"","receiver_name":"","tel":"","region":[]},
			codeImg:"",
			showConfirm:false,
			formData:{"mark":"","cash_on_deliver":0},
			showAddress:0,
			rules:{},
			defaultRegionCode:[]
		}
	},
	
	
	watch:{
		show(val){
			this.showBox=val;
			if(val){
				this.initData();
			}
		},
		
	
		showBox(val){
			this.$emit('update:show', val);
		},
		title(val){this.boxTitle=val;},

	},
	created() {
		_self = this;
		
	},
	
	computed:{
		regionName(){
			if(this.address.region){
				return this.address.region.map(item=>item.name).join(' ')
			}else{
				return "";
			}
		}
	},
	

	methods: {
		async initData(){
			let orderConfig = await request({url: '/api/order/getOptInfo',method: 'GET',data:{"order_id":this.optData.id}});
			if(orderConfig["product_area"]) this.areaList = orderConfig["product_area"];
			if(orderConfig["cerp_sync"]) this.syncList = orderConfig["cerp_sync"];
			if(orderConfig["address"]) this.address= orderConfig["address"];
			
			
		},
		
		handleGetRegion:function(region){
		
			this.address.region= region.map(item=>{
				return {
					name: item.name,
					code: item.code,
				}
			})
			
		},
		
		
	
		cancel(e){
			this.showBox=false;
		},
		confirmBox(e){
			
			if(this.showAddress){
				if(!this.address.region||this.address.region.length==0){
					uni.showToast({icon:"none",title:"请选择城市区域！"});
					return false;
				}
				if(!this.address.receiver_name){
					uni.showToast({icon:"none",title:"请填写收件人！"});
					return false;
				}
				if(!this.address.address){
					uni.showToast({icon:"none",title:"请填写详细地址！"});
					return false;
				}
				this.optData["address"]=this.address;
			}
			
			this.showConfirm=true;
		},
		submitForm(){
			
		},
		
		selectChange: function(e) {
			let index=e.detail.value;
			let key=e.currentTarget.dataset["key"];
			switch(key){
				case "product_area":
					this.areaIndex=index;
				break;
				case "cerp_sync":
					this.syncIndex=index;
				break;
			}
			
			if(this.syncList[this.syncIndex].val==1&&this.optData.cerp_sync_can){
				this.showAddress=1;
			}else{
				this.showAddress=0;
				
			}
			
		},
		
		confirmCode: function() {	

			this.optData["mark"]=this.formData["mark"];
			this.optData["cash_on_deliver"]=this.formData["cash_on_deliver"];
			
			if(this.optData.pro_type==1){
				this.optData["product_area"]=this.areaList[this.areaIndex].val;
			}
			this.optData["cerp_sync_need"]=this.syncList[this.syncIndex].val;
			let optInfo=Object.assign({"confirm":true},this.optData);
			this.showBox=false;
			
			this.$emit('update:optData',{});
			this.$emit('update:optData',optInfo);
		}

	}
}